$(document).ready(function(){


        $('.subcategory_link').on('click',function(){
            //alert(1);
            $('.active_sub_category').removeClass("active_sub_category").addClass("sub_category");
            $(this).parent().removeClass("sub_category").addClass("active_sub_category");
        });

        $('.link_to_main').on('click',function(){
            //window.location.hash = '';
        });

        $('.confirmation').on('click','button',function(){
            /*$(this).closest('.confirmation').find('.ticket').slideToggle();*/
            $('.door_detail').slideToggle();
        });


        $('.page_num').on('click',function(){
            $(".archive-pages>ul li").removeClass("selected");

            $(this).removeClass("page_num").addClass("selected");
        });


    $(".lupa_aa").fancybox({
        helpers: {
            title : {
                type : 'float'
            }
        }
    });
        /*$('img.lupa_a').mouseover(function() {
            //alert(1);
            $(this).removeClass("lupa_a").addClass("fancybox-thumbs");

            //$(this).attr('src','productimg/door 1 sm 1.jpg');

            $(this).parent().removeClass("lupa_aa").addClass("fancybox-thumbs");
        });

        $('img.lupa_a').mouseout(function() {
            //alert(1);
            $(this).removeClass("fancybox-thumbs").addClass("lupa_a");

            $(this).parent().removeClass("fancybox-thumbs").addClass("lupa_aa");
        });*/





        $('.sub_category').on('click',function(){
               // alert('sub');
            var subcategory = $(this).attr("id");
            var category = 'doors';
            var newhash;
            newhash = getDefaultHashTagForCategoryAndSubCategory(category,subcategory);
            //window.location.hash = newhash;


            });

        $('.previous').on('click',function(){

            var curr_page = $(".archive-pages .selected").attr('id');
            var prev_page;
            if (curr_page !=1){
                    prev_page = curr_page-1;
                    $(".archive-pages #"+curr_page).removeClass("selected");
                    $(".archive-pages #"+prev_page).addClass("selected");
                 }
            });

        $('.activeMenuItem').on('click',function(){

                //alert(1);
            });

            $(function() {
               // alert(1);

                // Binding a click event
                // From jQuery v.1.7.0 use .on() instead of .bind()
                $('#my-button').on('click', function() {
                    // Prevents the default action to be triggered.
                    //e.preventDefault();

                    // Triggering bPopup when click event is fired
                    var self = $(this) //button
                        , content = $('.content');
                   // alert(2);
                    $('element_to_pop_up').bPopup({
                        content:'iframe', //'ajax', 'iframe' or 'image'
                        contentContainer:'.content',
                        loadUrl:'http://dinbror.dk/search' //Uses jQuery.load()
                    });

                });

            });

                // Bind an event to window.onhashchange that, when the hash changes, gets the
                // hash and adds the class "selected" to any matching nav link.
          /* $(window).hashchange( function(){
               var correct_hash_pattern = /^((#mode=\[){1}(all|item){1}(]&category=\[){1}(doors|tables){1}(]&subcategory=\[){1}(all|entrancedoors|roomdoors|doorswithglass|wcdoors){1}(]&page=\[){1}([1-9])+[0-9]*(]&sort=\[){1}(default| var2){1}(]&itemno=\[){1}(|[1-9]+[0-9]*){1}(]){1})$|^$/;
               var hash = location.hash;
               //alert(hash);
              // alert(location.href);
               var res = hash.match(correct_hash_pattern);

               var correctHash = (res!=null);

               if(correctHash == false){
                   $.ajax(
                       {   url: "404.html",
                           success: function (result) {
                               //alert(result);
                               $(".section-content").html(result);
                           },
                           error: function(XMLHttpRequest, textStatus, errorThrown) {
                               alert("Status: " + textStatus); alert("Error: " + errorThrown);
                           }
                       });
                   $('.content-header').hide();
                   return;
               }else{

                   var regex;

                   regex = /(#mode=\[){1}(all|item){1}(]){1}/;
                   var mode = hash.match(regex);
                   mode = mode[0];
                   mode = mode.replace(/(#mode=\[){1}/,'');
                   mode = mode.replace(/(]){1}/,'');
                   //alert('mode = ' + mode);

                   regex = /(]&category=\[){1}(doors|tables){1}(]){1}/;
                   var category_t = hash.match(regex);
                   category_t = category_t[0];
                   category_t = category_t.replace(/(]&category=\[){1}/,'');
                   category_t = category_t.replace(/(]){1}/,'');
                   //alert('category = ' + category_t);

                   regex = /(]&page=\[){1}([1-9])+[0-9]*(]){1}/;
                   var page = hash.match(regex);
                   page = page[0];
                   page = page.replace(/(]&page=\[){1}/,'');
                   page = page.replace(/(]){1}/,'');
                   //alert('page = ' + page);


                   regex = /(]&itemno=\[){1}(|[1-9]+[0-9]*){1}(]){1}/;
                   var itemno = hash.match(regex);
                   itemno = itemno[0];
                   itemno = itemno.replace(/(]&itemno=\[){1}/,'');
                   itemno = itemno.replace(/(]){1}/,'');
                   //alert('itemno = ' + itemno);


               /*  //alert(hash);
                   // alert(hash=="#doors");
                    if(hash ==='#doors'){
                        //--------------
                        $('html').css('overflow','hidden');
                        $('body').css('overflow','hidden');

                        $( "#dialog-message" ).dialog({
                            modal: true,
                            resizable: false
                            ,width: "75px",
                            height: 120
                        });
                        $(".ui-dialog-titlebar").hide();
                        //--------

                        $.ajax(
                            {   url: "doors.html",
                                success: function (result) {
                                    //alert(result);
                                    $(".section-content").html(result);
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    alert("Status: " + textStatus); alert("Error: " + errorThrown);
                                }
                            });
                        //alert(1);
                        //setTimeout(function() {
                            $( "#dialog-message" ).dialog('close');
                            $('html').css('overflow','');
                            $('body').css('overflow','');
                        //}, 10);

                    }
                    else if(hash ===''){
                        alert('main');
                        $.ajax(
                            {   url: "main.html",
                                success: function (result) {
                                    //alert(result);
                                    $(".section-content").html(result);
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    alert("Status: " + textStatus); alert("Error: " + errorThrown);
                                }
                            });
                    }
                    else{
                        $('.section-content').html('else');
                    }

                    //hash.replace(/^#/, '' );--///
               }
                })

                // Since the event is only triggered when the hash changes, we need to trigger
                // the event now, to handle the hash the page may have loaded with.
                $(window).hashchange();*/


});
function getDefaultHashTagTemplate(){
    var hash_template = '#mode=[$mode]&category=[$category]&subcategory=[$subcategory]&page=[$page]&sort=[$sort]&itemno=[$itemno]'
    return hash_template;
}

function getDefaultHashTagForCategory(category){
    var newhash;
    var hash_template = getDefaultHashTagTemplate();
    newhash = hash_template;
   // alert(newhash);
    newhash = replaceDefaultCategoryInHashTag(newhash,category);
    //alert(newhash);
    newhash = replaceAllDefaultsInHashTag(newhash);
    //alert(newhash);

    return newhash;
}
function getDefaultHashTagForCategoryAndSubCategory(category,subcategory){
    var newhash;
    var hash_template = getDefaultHashTagTemplate();
    newhash = hash_template;
    newhash = replaceDefaultCategoryInHashTag(newhash,category);
    newhash = replaceDefaultSubCategoryInHashTag(newhash,subcategory);
    newhash = replaceAllDefaultsInHashTag(newhash);

    return newhash;
}
function replaceAllDefaultsInHashTag(hash){
    var category ='default';
    var subcategory = 'all';
    var mode = 'all';
    var page ='1';
    var itemno ='';
    var sort ='default';

    var newhash;

    newhash = hash;
    newhash = newhash.replace('$mode',mode);

    newhash = newhash.replace('$category',category);
    newhash = newhash.replace('$subcategory',subcategory);

    newhash = newhash.replace('$page',page);
    newhash = newhash.replace('$sort',sort);
    newhash = newhash.replace('$itemno',itemno);
    return newhash;
}
function replaceDefaultCategoryInHashTag(hash_template,category){
    var newhash;
    newhash = hash_template.replace('$category',category);
    return newhash;
}
function replaceDefaultSubCategoryInHashTag(hash_template,subcategory){
    var newhash;
    newhash = hash_template.replace('$subcategory',subcategory);
    return newhash;
}

/*function show_sub_items1(){
    var curr_category_name = $('.driled_link1').text();

    $('.sub_items1').slideToggle();
    $('.sub_items2').slideUp();
    $('.sub_items3').slideUp();
    $('.sub_items4').slideUp();

    //$('.curr_category h2').text(curr_category_name);
	

    //window.location.hash = newhash;
    //showDialog();
};
function show_sub_items2(){
    $('.sub_items2').slideToggle();
    $('.sub_items1').slideUp();
    $('.sub_items3').slideUp();
    $('.sub_items4').slideUp();
};
function show_sub_items3(){
    $('.sub_items3').slideToggle();
    $('.sub_items2').slideUp();
    $('.sub_items1').slideUp();
    $('.sub_items4').slideUp();
};
function show_sub_items4(){
    $('.sub_items4').slideToggle();
    $('.sub_items2').slideUp();
    $('.sub_items3').slideUp();
    $('.sub_items1').slideUp();
};
function show_sub_items5(){
    $('.sub_items5').slideToggle();
    $('.sub_items4').slideUp();
    $('.sub_items3').slideUp();
    $('.sub_items2').slideUp();
    $('.sub_items1').slideUp();
};*/
function btn1_click(){
    $('.section-content').hide();


    $('.product_info').show();

    $('.product_info').html( "yooohhoo");


};
function show_item_details(item_no){
    $('.catalog-section').hide();
    $('.content-header').hide();
    //window.location.hash = item_no;


    $('html').css('overflow','hidden');
    $('body').css('overflow','hidden');

    $( "#dialog-message" ).dialog({
        modal: true,
        resizable: false
        ,width: "75px",
        height: 120
    });


    $( "#dialog-message" ).dialog('close');
    $('html').css('overflow','');
    $('body').css('overflow','');

}

function change_hash(newhash){
   // window.location.hash = newhash;
};

function showDialog(){
    $('html').css('overflow','hidden');
    $('body').css('overflow','hidden');

    $( "#dialog-message" ).dialog({
        modal: true,
        resizable: false
        ,width: "75px",
        height: 120
    });
    $(".ui-dialog-titlebar").hide();
    setTimeout(function() {
        $( "#dialog-message" ).dialog('close');
        $('html').css('overflow','');
        $('body').css('overflow','');
    }, 2000);


}

//function test(){
//    $('.test').onclick(function(){
//
//
//    })
//}